#pragma once

#include <windows.h>
#include <string>
#include "Window.h"
#include "Thread.h"
#include <iostream>
#include <vector>

class RenderComponent;
/**
* An object that encapsulates an OpenGL window.
* @author Koen Samyn
*/
class OpenGLWindow: public Window,public IRunnable
{
public:
	/**
	* Creates a new OpenGLWindow with the provided title.
	* @param title the title of the OpenGL window.
	* @param width the width of the OpenGL window.
	* @param height the height of the OpenGL window.
	*/
	OpenGLWindow(std::wstring title,unsigned int width, unsigned int height);
	/**
	* Destroys the OpenGLWindow.
	*/
	virtual ~OpenGLWindow(void);
	/**
	* Initializes the open gl window.
	*/
	bool Init();
	/**
	* Creates the opengl window.
	*/
	bool Create();
	/**
	* Sets the fullscreen mode.
	* @param fullscreen true if the window should be fullscreen, false otherwise.
	*/
	void SetFullScreen(bool fullScreen);
	/**
	* Toggles the fullscreen state
	*/
	void ToggleFullScreen();
	/**
	* Repaints the window.
	*/
	void Repaint();
	/**
	* Kills this OpenGL window.
	*/
	bool Kill();
	/**
	* Resize the open gl window.
	* @param width the new width of the open gl window.
	* @param height the new height of the open gl window.
	*/
	void Resize(unsigned int width, unsigned int height);
	/**
	* Draws a grid
	*/
	void DrawGrid(int numberOfLines, float gridSize);
	/**
	* Draw the open gl window.
	*/
	void Draw();
	//void DrawCube(float l,float w,float h);
	//void Vertex(float3& v, float2& uv);

	/**
	* A key pressed event was received.
	* @param key the character that was pressed.
	*/
	virtual void KeyPressed(int keyCode){}
	/**
	* A key released event was received.
	* @param key the character that was released.
	*/
	virtual void KeyReleased(int keyCode){
		if ( keyCode == 'A' ){
			ToggleFullScreen();
		}
	}

	virtual void KeyTyped(wchar_t keyCode){
		std::cout << "Key was :" << keyCode;
	}

	void Run();
private:
	/**
	* Acquire the lock for painting.
	*/
	void AcquirePaintLock();
	/**
	* Leave the lock for painting.
	*/
	void ReleasePaintLock();
	/**
	* Go to full screen mode
	*/
	void SwitchToFullscreen();
	/**
	* Go to windowed mode
	*/
	void SwitchToWindowed();
	/**
	* Is the window fullscreen or not. Initially set to false.
	*/
	bool m_FullscreenState;
	/**
	* The requested full screen state.
	*/
	bool m_Fullscreen;
	/**
	* The critical section object for the paint thread.
	*/
	CRITICAL_SECTION m_PaintCS;
	/**
	* The bitdepth for the window.
	*/
	unsigned int m_Bits;

	Thread* m_pPaintThread;
	bool m_Running;

	std::vector<RenderComponent*> m_RenderComponents;
};

